﻿//Static:
function initializeGoogleMaps() {
	$(".GMapArea").each(function() {
		var me = $(this);
		if (me.data("gmap") == null) {
			var gmap = new google.maps.Map2(this);
			me.data("gmap", gmap);
			if (me.hasClass("FullPanZoom"))
				gmap.addControl(new GLargeMapControl());
			if (me.hasClass("FullMapping"))
				gmap.addControl(new GMapTypeControl());

			var zoom = 13;
			if (me.metadata().zoom)
				zoom = me.metadata().zoom;
			else
				me.metadata().zoom = zoom;

			var lat = me.metadata().lat;
			var lng = me.metadata().lng;
			if (lat != null && lng != null) {
				me.data('firstRef', new google.maps.LatLng(new Number(lat), new Number(lng)));
				gmap.setCenter(me.data('firstRef'), zoom);
			}
			else {
				gmap.setCenter(new google.maps.LatLng(10.318244232292992, 123.88848781585693), zoom);
				if (me.metadata().location != null) {
					var gc = new GClientGeocoder();
					gc.getLatLng(me.metadata().location, function(pt) {
						if (pt != null) {
							me.data('firstRef', pt);
							gmap.setCenter(pt);
						}
					});
				}
			}
			GEvent.addListener(gmap, "infowindowbeforeclose", function() {
				if (beforeInfoWindowClose != null)
					beforeInfoWindowClose();
			});
			var mapSetup = eval(this.id + "Setup");
			if (mapSetup)
				mapSetup(me);
		}
	});
}

var GlobalGoogleMapIcons = {	}

function refreshGMaps() {
	$('.GMapArea').each(function () {
		var me = $(this);
		me.data('gmap').checkResize();
		if (me.data('firstRef')) {
			me.data('gmap').setCenter(me.data('firstRef'), me.metadata().zoom);
			me.data('firstRef', null);
		}
	});
}

//Members:
function setGMapCenterOnLocation(me, loc) {
	var gc = new GClientGeocoder();
	gc.getLatLng(loc, function(pt) {
		if (pt != null) {
			me.data('gmap').setCenter(pt);
		}
	});
}

function createTabbedMarker(me, point, BasicContent, DetailContent, icon, click, onclose) {
	if (me.data('markers') == null) {
		me.data('markers', new Array());
		me.data('curmark', 0);
	}
	me.data('markers').push({ point: point });
	BasicContent += '<br> <a href="http://maps.google.com/maps?saddr=&daddr=' + point.toUrlValue() + '" target ="_blank">Get Directions To Here!</a>';
	var options = null;
	if (icon != null)
		options = { icon: icon };
	var marker = new GMarker(point,options);
	GEvent.addListener(marker, "click", function() {
		var showEmbeddedInScript = false;
		var myClick = click;
		if (click.indexOf("$_showTabsScript$") != -1) {
			showEmbeddedInScript = true;
			myClick = click.replace("$_showTabsScript$", 'marker.openInfoWindowTabs([new GInfoWindowTab("Basic Info", BasicContent), new GInfoWindowTab("Detailed Info", DetailContent)]);');
		}
		eval(myClick);
		if (!showEmbeddedInScript)
			marker.openInfoWindowTabs([new GInfoWindowTab("Basic Info", BasicContent), new GInfoWindowTab("Detailed Info", DetailContent)]);
	});
	return marker;
}

function moveToMapMarker(me, next) {
	var markers = me.data('markers');
	if (markers) {
		var pos = me.data('curmark');
		if ((next ? ++pos : --pos) == (next ? markers.length : -1))
			pos = (next ? 0 : (markers.length - 1));
		var marker = markers[pos];
		me.data('curmark', pos);
		me.data('gmap').setCenter(marker.point);
	}
}